﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "CmdCreateStructureCustomizedProfileBeamFamilyDocument.h"
#include "GmUiStructureCommandRegister.h"
#include "StructureDefaultFamilyNames.h"
#include "StructureCmdIds.h"
#include "CmdStatusUtils.h"
#include "IModelView.h"
#include "GbmpModelViewTypes.h"
#include "UiCommonDialog.h"
#include "UserTransactionGroupUtils.h"
#include "IUserTransaction.h"
#include "StructureEditModeTypes.h"
#include "IEditMode.h"
#include "INewViewProfileEditModeBehavior.h"
#include "IUiDocument.h"
#include "IMainWindow.h"
#include "IUiDocumentViewManager.h"
#include "ICommandManager.h"
#include "UiDocumentViewUtils.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

REGISTER_GM_COMMAND(CmdCreateStructureCustomizedProfileBeamFamilyDocument)

CmdCreateStructureCustomizedProfileBeamFamilyDocument::CmdCreateStructureCustomizedProfileBeamFamilyDocument()
    : GbmpCommandBase(ID_CMD_CREATE_STRAIGHT_STRUCTURE_CUSTOMIZED_PROFILE_BEAM_FAMILY_DOCUMENT)
{
}

bool CmdCreateStructureCustomizedProfileBeamFamilyDocument::IsEnabled() const
{
    CMD_ONLY_ENABLE_IN_PROJECT_ENVIRONMENT
}

bool CmdCreateStructureCustomizedProfileBeamFamilyDocument::IsVisible() const
{
    CMD_ONLY_VISIBLE_IN_PROJECT_ENVIRONMENT
}

bool gcmp::CmdCreateStructureCustomizedProfileBeamFamilyDocument::ShouldClearSelectionBeforeExecution() const
{
    return true;
}

OwnerPtr<IAction> CmdCreateStructureCustomizedProfileBeamFamilyDocument::ExecuteCommand(const CommandParameters& cmdParams)
{
    IDocument* pDoc = UiDocumentViewUtils::GetCurrentDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");

    IUiDocument* pUIDoc = UiDocumentViewUtils::GetCurrentUiDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");

    IModelView* pModelView = UiDocumentViewUtils::GetCurrentModelView();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pModelView, L"pModelView为空",L"GDMPLab",L"2024-03-30");

    const std::wstring& viewType = pModelView->GetUserDataId();

    if (PlanViewType != viewType && View3DViewType != viewType)
    {
        UiCommonDialog::ShowMessageBox(GBMP_TR(L"绘制梁"), GBMP_TR(L"此视图下不支持绘制梁"), (int)UiCommonDialog::ButtonType::OK);
        return nullptr;
    }

    // 事务组
    int groupId;
    UserTransactionGroupUtils::Start(pDoc, GBMP_TR(L"创建自定义截面梁"), groupId);

    OwnerPtr<IUserTransaction> ut = IUserTransaction::Create(pDoc, GBMP_TR(L"开始绘制轮廓"));

    IEditMode* pProfileEditMode = IEditMode::Get(pDoc, StructureEditModeTypes::NewViewCustomizedProfileModeType);
    if (!pProfileEditMode)
    {
        pProfileEditMode = IEditMode::Create(pDoc, INewViewProfileEditModeBehavior::Create());
    }
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pProfileEditMode, L"pProfileEditMode为空",L"GDMPLab",L"2024-03-30");

    // 设置默认的族名
    INewViewProfileEditModeBehavior* pProfileEditModeBehavior = quick_cast<INewViewProfileEditModeBehavior>(pProfileEditMode->GetEditModeBehavior());
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pProfileEditModeBehavior, L"pProfileEditModeBehavior为空",L"GDMPLab",L"2024-03-30");
    std::wstring familyName(DEFAULT_CUSTOMIZED_SECTION_BEAM_FAMILY_NAME);
    pProfileEditModeBehavior->SetEditingFamilyName(familyName);
    pProfileEditModeBehavior->SetGuiApplyCancelCmdIds({ ID_CMD_EDIT_STRUCTRUE_CUSTOMIZED_PROFILE_BEAM_FINISH ,ID_CMD_EDIT_PROFILE_CANCEL });

    pProfileEditMode->StartEditing(pDoc, ElementId::InvalidID, true, false);
    ut->Commit();

    // 打开新建视图
    ElementId newModelViewId = pProfileEditModeBehavior->GetModelViewId();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(newModelViewId.IsValid(), L"tempModelViewId无效",L"GDMPLab",L"2024-03-30");
    IUiDocumentViewManager* pUIDocumentViewManager = IUiDocumentViewManager::Get();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pUIDocumentViewManager, L"pUIDocumentViewManager为空",L"GDMPLab",L"2024-03-30");
    IMainWindow* pMainWindow = IMainWindow::GetMainWindow();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pMainWindow, L"pMainWindow为空",L"GDMPLab",L"2024-03-30");
    IUiView* pUIView = pMainWindow->CreateOrOpenUiView(pUIDoc->GetId(), newModelViewId, false, L"");
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pUIView, L"pUIView为空",L"GDMPLab",L"2024-03-30");

    // 从这里会进到 UINewViewCustomizedProfileEditModeBehavior::SetupUi(params) 中，更新UIEditMode
    pUIDoc->UpdateView();

    // 启动画轮廓线的命令
    ICommandManager* pCommandManager = ICommandManager::Get();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pCommandManager, L"pCommandManager为空",L"GDMPLab",L"2024-03-30");
    pCommandManager->SendCommand(ID_CMD_STRUCTURE_DRAW_STRAIGHT_PROFILE);

    return nullptr;
}
